<?php
require_once('dataaccess.php');


$packageid = $_POST['p'];
$locid = $_POST['l'];
$usrpkg = $_POST['usrp'];

doCrazyChartStuff($packageid,$locid,$usrpkg);


/**
 * Create chart data
 */
function doCrazyChartStuff($packageid, $locid, $usrpkg){
	$rows = array();
	$package = '';
	$location = '';

	$location = getLocationNameById($locid);

	$rs = getSpeedsByPackageAndLocation($usrpkg, $locid);
	$urows = getSpeedTimeArray($rs);

	if ($packageid > 0){
		$rows = getSpeedsByPackageAndLocation($packageid, $locid);
		$p1 = getSpeedTimeArray($rows);
		$package = getPackageNameById($packageid);
		$response = array('dn'=>$package,'d'=>$p1,'u' => $urows,'t'=>'Speedtest for '.$package.' in '.$location);
	} else {/*
		$rows = getSpeedsByPackageAndLocation(1, $locid);
		$p1 = getSpeedTimeArray($rows );

		$rows = getSpeedsByPackageAndLocation(2, $locid);
		$p2 = getSpeedTimeArray($rows);
		*/
		
		$rows = getAllResults();
		$p1 = getSpeedTimeArray($rows);

		//$response = array('d'=>$p1,$p2,'t'=>'Speedtest for '.$location);
		$response = array('d'=>$p1,'t'=>'Speedtests, everywhere');
	}
	echo json_encode($response);
}

/**
 * Function to get speeds and times as an array
 * @param a $rows
 * @return array
 */
function getSpeedTimeArray($rows){
	$ret = array();
	foreach($rows as $row){
		$ret[] = array(strtotime($row['time'])*1000,(int)$row['down']);
	}
	return $ret;
}

?>